package com.example.dubbo.consumer.config;

import com.alibaba.fastjson.JSON;

import com.example.dubbo.api.dubbo.PeopleService;
import com.example.dubbo.api.entity.PeopleEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/9/18 13:38
 */
@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {

    @DubboReference(application = "dubbo-provider-9009",version = "2.0.0")
    public PeopleService peopleService;

    @Override
    public void run(String... args) throws Exception {

        CompletableFuture future = CompletableFuture.runAsync(() -> {
            while (true) {
                if (peopleService != null) {
                    int nextInt = new Random().nextInt(10);
                    PeopleEntity people = peopleService.getPeopleById(nextInt);
                    String jsonString = JSON.toJSONString(people);
                    log.info("people:{}",jsonString);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    log.error("peopleService is null  ");
                }
            }

        });

    }
}
